from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import AI_Consultation_schema
from controller import AI_Consultation_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/AI_Consultation", response_model=AI_Consultation_schema.Ai_consultation, tags=["AI_Consultation"])
async def create_AI_Consultation(AI_Consultation: AI_Consultation_schema.Ai_consultation, db: Session = Depends(get_db)):
    return AI_Consultation_controller.create_AI_Consultation(db, AI_Consultation)


@router.get("/AI_Consultation/{ID}", response_model=AI_Consultation_schema.Ai_consultation, tags=["AI_Consultation"])
async def get_AI_Consultation(ID: int, db: Session = Depends(get_db)):
    db_AI_Consultation = AI_Consultation_controller.get_AI_Consultation(db, ID)
    if db_AI_Consultation is None:
        raise HTTPException(status_code=404, detail="AI_Consultation not found")
    return db_AI_Consultation


@router.get("/AI_Consultation", response_model=List[AI_Consultation_schema.Ai_consultation], tags=["AI_Consultation"])
async def get_all_AI_Consultation(db: Session = Depends(get_db)):
    all_db_AI_Consultation = AI_Consultation_controller.get_all_AI_Consultation(db)
    if all_db_AI_Consultation is None:
        raise HTTPException(status_code=404, detail="0 AI_Consultation found, empty table")
    return all_db_AI_Consultation


@router.put("/AI_Consultation/{ID}", tags=["AI_Consultation"])
async def update_AI_Consultation(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_AI_Consultation = AI_Consultation_controller.update_AI_Consultation(db, ID, field_name, field_value)
        if db_AI_Consultation is None:
            raise HTTPException(status_code=404, detail="AI_Consultation not found")
        return db_AI_Consultation
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/AI_Consultation/{ID}", tags=["AI_Consultation"])
async def delete_AI_Consultation(ID: int, db: Session = Depends(get_db)):
    is_deleted = AI_Consultation_controller.delete_AI_Consultation(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
